/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs.admin;

import daos.EditorialCategoryDAO;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.*;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class EditorialCategoryGrid extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    String[] headers = new String[]{"EditorialCatId", "Name"};
    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */

    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            int currentPage = 0;
            try {
                currentPage = Integer.parseInt(pageContext.getRequest().getParameter(WebConst.PAGE));
            } catch (Exception e) {
                currentPage = 0;
            }
            //set current page
            currentPage = (currentPage == 0 ? 1 : currentPage);

            writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
            writer.println("<tr>");
            //print header
            for (int i = 0; i < headers.length; i++) {
                writer.println("<td>" + headers[i] + "</td>");
            }
            writer.println("<td>&nbsp;</td>");
            writer.println("<td>&nbsp;</td>");
            writer.println("</tr>");

            int totalRecord = EditorialCategoryDAO.singleton().recordCount();

            //print data
            ArrayList<EditorialCategory> lstEditorialCategories = new ArrayList<EditorialCategory>();

            lstEditorialCategories = EditorialCategoryDAO.singleton().selectAll(currentPage);
            for (EditorialCategory editorialCategory : lstEditorialCategories) {
                writer.println("<tr>");
                writer.println("<td>" + editorialCategory.getEditorialCatId() + "</td>");
                writer.println("<td>" + editorialCategory.getName() + "</td>");
                writer.println("<td>"
                        + "<a href='AdminEditorialCategoryView.jsp?action=edit&editorcatid="
                        + editorialCategory.getEditorialCatId() + "'>" + WebConst.EDIT + "</a></td>");
                writer.println("<td>"
                        + "<a href='AdminEditorialCategoryServlet?action=delete&editorialcatid="
                        + editorialCategory.getEditorialCatId() + "'>" + WebConst.DELETE + "</a></td>");
                writer.println("</tr>");
            }

            //print insert new
            writer.println("<tr>");
            int colSpan = headers.length + 2;
            writer.println("<td colspan="+colSpan+">"
                    + "<a href='AdminEditorialCategoryView.jsp?action=new'>" + WebConst.NEW + "</a>");

            //print paging
            int numPage = totalRecord / WebConst.ROW_PER_PAGE;
            numPage = (totalRecord % WebConst.ROW_PER_PAGE>0)?numPage+1:numPage;
            if (numPage > 1) {
                int p = 0;
                for (int i = 0; i < numPage; i++) {
                    p=i+1;
                    if (currentPage == p) {
                        writer.print("&nbsp;" + p);
                    } else {
                        writer.print("&nbsp;"
                                + "<a href='AdminEditorialCategoryView.jsp?action=view"
                                + "&page=" + p + "'>" + p + "</a>");
                    }
                }
            }
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("</table>");
        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
